package com.controls.ws;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;
import com.objects.Contact;
import com.objects.ContactCheck;
import com.objects.ContactInfor;

public class ContactWSControl extends BaseWSControl {
	public ContactWSControl(Context cxt, WebServiceCommunicatorListener lsn) {
		super(cxt, lsn);
	}

	public boolean getContacts(String accessToken) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetContacts xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "</GetContacts>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_CONTACT, content);
		return true;
	}

	public boolean getContactInfor(String accessToken, long contactID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetContactInfo xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<ContactID>"
				+ contactID + "</ContactID>" + "</GetContactInfo>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_CONTACT_INFOR, content);
		return true;
	}

	public boolean deleteContacts(String accessToken, String contact) {

		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<DeleteContactsFromList xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<Contacts>" + contact
				+ "</Contacts>" + "</DeleteContactsFromList>" + "</soap:Body>"
				+ "</soap:Envelope>";

		fetch(WebServiceFlag.DELETE_CONTACT, content);
		return true;

	}

	public boolean addContactsExtend(String accessToken, String email,
			String firstName, String lastName, String SMSCC, String SMS,
			String accessCode, String smsCountryName, String landCountryName,
			String landCountryCode, String landCountryNumber, String notes) {

		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<AddContact_Ext xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>"
				+ accessToken
				+ "</Token>"
				+ "<ContactEmail>"
				+ email
				+ "</ContactEmail>"
				+ "<ContactFirstname>"
				+ firstName
				+ "</ContactFirstname>"
				+ "<ContactLastname>"
				+ lastName
				+ "</ContactLastname>"
				+ "<ContactSMSCC>"
				+ SMSCC
				+ "</ContactSMSCC>"
				+ "<ContactSMS>"
				+ SMS
				+ "</ContactSMS>"
				+ "<AccessCode>"
				+ accessCode
				+ "</AccessCode>"
				+ "<ContactSMSCountryName>"
				+ smsCountryName
				+ "</ContactSMSCountryName>"
				+ "<LandLineCountryName>"
				+ landCountryName
				+ "</LandLineCountryName>"
				+ "<LandLineCountryCode>"
				+ landCountryCode
				+ "</LandLineCountryCode>"
				+ "<LandLinePhoneNumber>"
				+ landCountryNumber
				+ "</LandLinePhoneNumber>"
				+ "<ContactNotes>"
				+ notes
				+ "</ContactNotes>"
				+ "</AddContact_Ext>" + "</soap:Body>" + "</soap:Envelope>";
		Log.e("TAG_ADD_CONTACT", content);
		fetch(WebServiceFlag.ADD_CONTACT_EXTEND, content);
		return true;

	}

	public boolean addContact(String accessToken, String email,
			String firstName, String lastName) {

		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<AddContact xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<ContactEmail>"
				+ email + "</ContactEmail>" + "<ContactFirstname>" + firstName
				+ "</ContactFirstname>" + "<ContactLastname>" + lastName
				+ "</ContactLastname>" + "</AddContact>" + "</soap:Body>"
				+ "</soap:Envelope>";
		Log.e("TAG_ADD_CONTACT", content);
		fetch(WebServiceFlag.ADD_CONTACT, content);
		return true;

	}

	public boolean sendAdvisory(String accessToken, long contactID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<SendAdvisory xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<ContactID>"
				+ contactID + "</ContactID>" + "</SendAdvisory>"
				+ "</soap:Body>" + "</soap:Envelope>";
		fetch(WebServiceFlag.SEND_ADVISORY, content);
		return true;

	}

	/**
	 * 
	 * @param xml
	 * @return
	 */

	public ArrayList<ContactCheck> parseContactCheck(String xml) {
		ArrayList<ContactCheck> items = new ArrayList<ContactCheck>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetContactsResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					ContactCheck item = new ContactCheck();
					if (obj.has("ContactId")) {
						item.setID(obj.getLong("ContactId"));
					}
					if (obj.has("EmailAddress")) {
						item.setEmailAddress(obj.getString("EmailAddress"));
					}
					if (obj.has("Name")) {
						item.setName(obj.getString("Name"));
					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}

	public ArrayList<Contact> parseContact(String xml) {
		ArrayList<Contact> items = new ArrayList<Contact>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetContactsResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					Contact item = new Contact();
					if (obj.has("ContactId")) {
						item.setID(obj.getLong("ContactId"));
					}
					if (obj.has("EmailAddress")) {
						item.setEmailAddress(obj.getString("EmailAddress"));
					}
					if (obj.has("Name")) {
						String name = obj.getString("Name");
						item.setName(name);
						String subString[] = name.split(",");
						if (subString.length > 0)
							item.setFirstName(subString[0]);
						if (subString.length > 1)
							item.setLastName(subString[1]);

					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}

	public ContactInfor parseContactInfor(String xml) {
		ContactInfor item = null;
		Document doc = getDomElement(xml);
		if (doc == null) {
			return item;
		}

		NodeList results = doc.getElementsByTagName("GetContactInfoResult");
		JSONObject mObject = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObject = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObject != null) {
			try {
				item = new ContactInfor();
				if (mObject.has("ContactFirstname")) {
					item.setFirstName(mObject.getString("ContactFirstname"));
				}
				if (mObject.has("ContactLastname")) {
					item.setLastName(mObject.getString("ContactLastname"));
				}
				if (mObject.has("ContactNotes")) {
					item.setNotes(mObject.getString("ContactNotes"));
				}
				if (mObject.has("ContactSMS")) {
					item.setCellNumber(mObject.getString("ContactSMS"));
				}
				if (mObject.has("ContactSMSCC")) {
					item.setCellCountryCode(mObject.getString("ContactSMSCC"));
				}
				if (mObject.has("ContactSMSCountryName")) {
					item.setCellCountryName(mObject
							.getString("ContactSMSCountryName"));
				}
				if (mObject.has("LandLineCountryCode")) {
					item.setLandCountryCode(mObject
							.getString("LandLineCountryCode"));
				}
				if (mObject.has("LandLineCountryName")) {
					item.setLandCountryName(mObject
							.getString("LandLineCountryName"));
				}
				if (mObject.has("LandLinePhoneNumber")) {
					item.setLandNumber(mObject.getString("LandLinePhoneNumber"));
				}
				if (mObject.has("AccessCode")) {
					item.setAccessCode(mObject.getString("AccessCode"));
				}
				if (mObject.has("ContactEmail")) {
					item.setEmail(mObject.getString("ContactEmail"));
				}
			} catch (JSONException e) {
			}
		}
		return item;
	}

	public String parseAction(String xml, String key) {
		String returnValue = null;
		Document doc = getDomElement(xml);
		if (doc == null) {
			return returnValue;
		}

		NodeList results = doc.getElementsByTagName(key);
		JSONObject mObj = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObj = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObj != null) {
			try {

				if (mObj.has("ReturnValue")) {
					returnValue = mObj.getString("ReturnValue");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnValue;
	}
}
